package com.kh.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kh.pojo.AjaxJson;
import com.kh.pojo.RolePoJo;
import com.kh.pojo.TableJson;
import com.kh.service.UserService;
import com.kh.utils.Tools;
import com.kh.utils.UserUtils;

@Controller
@RequestMapping("menuController")
public class MenuController {
	@Autowired
	UserService userService;

	@RequestMapping(params = "roleList")
	public String roleList() {
		return UserUtils.THEME + "roleList";
	}

	@RequestMapping(params = "roleAdd")
	public String roleAdd() {
		return UserUtils.THEME + "role-add";
	}

	@RequestMapping(params = "roleDataGrid")
	@ResponseBody
	public TableJson<RolePoJo> roleDataGrid() {
		TableJson<RolePoJo> json = new TableJson<>();
		List<RolePoJo> roleList = userService.getRoleList();
		json.setData(roleList);
		return json;
	}

	@RequestMapping(params = "doRoleAdd")
	@ResponseBody
	public AjaxJson doRoleAdd(RolePoJo role) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.setCode(AjaxJson.ok);
		String name = role.getName();
		String code = role.getCode();
		if(!StringUtils.isBlank(name)&&!StringUtils.isBlank(code)) {
			userService.saveRole(role);
			
		}else {
			ajaxJson.setCode(AjaxJson.error);
			ajaxJson.setMessage("编码或者名称有一个是无效的");
		}
		return ajaxJson;
	}
	

}
